package com.eastfair.es.util;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.es.dto.EsPage;
import com.eastfair.es.vo.AllLogOfEs;
import com.eastfair.es.vo.AllLogSimpleVO;
import com.eastfair.es.vo.AllLogVO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EsWhereUtil {


    /**
     * 属性 值等于和包含
     *
     * @param fieldName
     * @param value
     * @return
     */
    public static String matchWhere(String fieldName, Object value) {
        if (StringUtils.isBlank(fieldName) || value == null) {
            return null;
        }
/*
        "multi_match": {
                "type": "cross_fields",
                        "query": "f7a86e720d0e49918fc5fdd227d4608c",
                        "fields": [
                "traceId"
                ],
                "operator": "and"
            }
        }
        {
            "multi_match": {
            "type": "cross_fields",
                    "query": "6a95519452bf410690f5b94c0aee9565",
                    "fields": [
            "traceId"
                            ],
            "operator": "or"
        }
        }*/

        return String.format("{\"multi_match\":{\"type\":\"cross_fields\",\"query\":\"%s\",\"fields\":[\"%s\"],\"operator\":\"and\"}}", value, fieldName);
    }


    /**
     * 时间 大于小于
     *
     * @param fieldName
     * @param startTime
     * @param endTime
     * @return
     */
    public static String rangeWhere(String fieldName, Date startTime, Date endTime) {
        if (StringUtils.isBlank(fieldName) || startTime == null || endTime == null) {
            return null;
        }

       /* {
                        "range": {
                            "dtTime": {
                                "gte": 1676110434000,
                                "lt": 1676215003000
                            }
                        }
                    }*/

        return String.format("{\"range\":{\"%s\":{\"gte\":%s,\"lt\":%s}}}", fieldName, startTime.getTime(), endTime.getTime());
    }


    /**
     * 多少条开始
     *
     * @param esPage
     * @return
     */
    public static String queryFrom(EsPage esPage) {
        Long from = (esPage.getCurrent() - 1) * esPage.getSize();
        return String.valueOf(from);
    }


    /**
     * 条数
     *
     * @param esPage
     * @return
     */
    public static String querySize(EsPage esPage) {
        return String.valueOf(esPage.getSize());
    }


}
